<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View extends CI_Controller {

	function View(){
		parent::__construct();
		
		$this->load->model('cms_model');
	}
	
	public function index(){
		//param
		$sid = $this->uri->segment(3);
		if(empty($sid))
			$sid = 's_001';
		
		//find template
		$tarray = $this->cms_model->findtemplate($sid, 0);
		$tid = $tarray['tid'];
		$tcontent = $tarray['content'];
		$sid = $tarray['sid'];
		
		if(empty($tcontent))
			return ;
		
		/*---the data to view---*/
		
		//today
		$data['today'] = $this->cms_model->gettodayandweekstr();
		
		//menu columns
		$data['menucols'] = $this->cms_model->listmenucols($sid, 'menu', 0);
		
		//网上公示 menu link
		$data['linkpubnews']= $this->cms_model->getcollinkbyname('网上公示', 0);
		$data['linkfaq']= $this->cms_model->getcollinkbyname('常见问题', 0);
		
		//图片新闻
		$data['picnews']= $this->cms_model->getpicnews( 0);
		
		//领导简介
		$data['leaders']= $this->cms_model->getleaders( 0);
		
		//机关建设
		$data['jiguan']= $this->cms_model->listsubcolumnsbycname('机关建设', 0);
		
		//处室子站
		$data['chushi']= $this->cms_model->listsubcolumnsbycname('处室子站', 0);
		
		//商务新闻
		$data['biznews']= $this->cms_model->listcolarticlesbycname('商务新闻',1, 6, 0);
		$data['linkbiznews'] = $this->cms_model->getcollinkbyname('商务新闻', 0);
		
		//工作动态
		$data['dongtai']= $this->cms_model->listcolarticlesbycname('工作动态',1, 6, 0);
		$data['linkdongtai'] = $this->cms_model->getcollinkbyname('工作动态', 0);
		
		//领导讲话
		$data['lingdao']= $this->cms_model->listcolarticlesbycname('领导讲话',1, 6, 0);
		$data['linklingdao'] = $this->cms_model->getcollinkbyname('领导讲话', 0);
		
		//通知公告
		$data['tongzhi']= $this->cms_model->listcolarticlesbycname('通知公告',1, 6, 0);
		$data['linktongzhi'] = $this->cms_model->getcollinkbyname('通知公告', 0);
		
		
		/*----------------------*/		
		
		//view by smarty
		$tplfilename = $tid.'.tpl';
		$this->smarty->view($tplfilename,$data,False);		
		//$output = $this->smarty->view($tplfilename,$data,True);
		//echo ($output);
	}
	
	public function col(){
		//param
		$cid = $this->uri->segment(3);
		$pagenum = $this->uri->segment(4);
		//$tag = $this->uri->segment(5); //special process
		
		//find template
		$sid = '';
		$tid='';
		$tcontent = '';
		$tarray = $this->cms_model->findtemplate($cid, 0);
		$tid = $tarray['tid'];
		$tcontent = $tarray['content'];
		$sid = $tarray['sid'];
		if(empty($tcontent))
			return ;
			
		/*---the data to view---*/
			
		//today
		$data['today'] = $this->cms_model->gettodayandweekstr();
		
		//
		$data['cid'] = $cid;
			
		//menu columns
		/*$sql = "select * from qf_column where parentid='' and sid='$sid' and disabled='0' and display='0' and tag='menu' order by sortno asc";
		$menucols = $this->db->query($sql);
		$data['menucols'] = $menucols->result_array();
		
		//template format
		{section name=sec_menu loop=$menucols}
        	<a href="/view/col/{$menucols[sec_menu].cid}">{$menucols[sec_menu].cname}</a>
        {/section}
          
		*/
		$data['menucols'] = $this->cms_model->listmenucols($sid, 'menu', 0);
						
		//nav column
		$navcolarray = $this->cms_model->getnavcols($cid,0);
		$data['navcolarray'] = $navcolarray;
		
		//column name
		$colname = $this->cms_model->getcolname($cid);
		$data['colname'] = $colname;	
		
		//sub columns
		/*$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$cid' order by sortno";
		$querysubcol = $this->db->query($sql);
		$data['subcol'] = $querysubcol->result_array();*/
		$data['subcol'] = $this->cms_model->listsubcolumns($cid, 0);
		
		//articles
		if(empty($pagenum))
			$pagenum = 1;
		$pagesize = 15;		
		$start = $pagesize * ($pagenum-1);
		
		/*$sql = "select * from qf_article where cid='$cid' and disabled='0' and display='0' and astatus='1' order by sortno asc, updatetime desc";
		$sql .=" limit ".$start.",".$pagesize;
		$queryarts = $this->db->query($sql);
		$data['articles'] = $queryarts->result_array();*/
		
		$data['articles'] = $this->cms_model->listcolarticles($cid, $pagenum, $pagesize, 0);
		
		//pager
		$pagecount = 0;		
		$rownum = 0;
		$sql = "select count(*) as c from qf_article where cid='$cid' and disabled='0' and display='0' and astatus='1' ";
		$querycount = $this->db->query($sql);
		if($querycount->num_rows() > 0){
			$row = $querycount->row();
			$rownum = $row->c;
		}
		$pagecount = ceil($rownum / $pagesize);			
		$pager = $this->cms_model->GetPagerHtml($pagenum,$pagecount);
		$data['pager'] = $pager;
		
		/*----------------------*/		
		
		//view by smarty
		$tplfilename = $tid.'.tpl';
		$this->smarty->view($tplfilename,$data,False);
	}
	
	public function art(){
		//param
		$aid = $this->uri->segment(3);
		
		/*---the data to view---*/
		//today
		$xingqi = $this->cms_model->getweekstr();
		$today = date('Y年m月d日').' '.$xingqi;
		$data['today'] = $today;
		
		//
		$sid = '';
		$cid = '';
		$tid='';
		$tcontent = '';
		$atitle='';
		$acontent = '';
		$atype = '';
		$updatetime = '';
		
		$sql = "select * from qf_article where aid='$aid'";
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$row = $query->row();
			$sid = $row->sid;
			$tid = $row->templateid;
			$acontent = $row->acontent;
			$author = $row->author;
			$updatetime = $row->updatetime;
			$atype = $row->atype;
			$cid = $row->cid;
			$atitle = $row->title;
		}
		
		$data['acontent'] = $acontent;
		$data['author'] = $author;
		$data['updatetime'] =  date('Y年m月d日', strtotime($updatetime));;
		$data['atitle'] = $atitle;
		
		//find template
		$tarray = $this->cms_model->findtemplate($aid, 0);
		$tid = $tarray['tid'];
		$tcontent = $tarray['content'];
		$sid = $tarray['sid'];
		
		if(empty($tcontent))
			return ;
			
		$data['cid'] = $cid;
			
		//menu columns
		$data['menucols'] = $this->cms_model->listmenucols($sid, 'menu', 0);
						
		//column name
		$colname = $this->cms_model->getcolname($cid);
		$data['colname'] = $colname;
		
		/*----------------------*/		
		
		//view by smarty
		$tplfilename = $tid.'.tpl';
		$this->smarty->view($tplfilename,$data,False);
	}
}
